package com.ly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.entity.Member;
import com.ly.entity.Student;
import com.ly.mapper.MemberMapper;
import com.ly.mapper.StudentMapper;
import com.ly.mapper.TeamMapper;
import com.ly.pojo.dto.MemberAddDTO;
import com.ly.pojo.dto.MemberDTO;
import com.ly.pojo.vo.MemberBelongVO;
import com.ly.service.MemberService;
import com.ly.utils.PageData;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author : 黑渊白花
 * @ClassName MemberServiceImpl
 * @date : 2025/2/8 22:32
 * @Description
 */
@Service
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeamMapper teamMapper;

    @Override
    public IPage<MemberBelongVO> getAllMemberPage(MemberDTO memberDTO) {
        Integer pageNum = memberDTO.getPageNum();
        Integer pageSize = memberDTO.getPageSize();
        String teamName = memberDTO.getTeamName();
        String userName = memberDTO.getUserName();
        // 使用 MyBatis-Plus 的分页查询
        // Page<MemberBelongVO> page = new Page<>(pageNum, pageSize);
        // 调用 MyBatis-Plus 的分页查询方法，这样可以自动处理分页和总数
        IPage<MemberBelongVO> page = new Page<>(pageNum,pageSize);
        IPage<MemberBelongVO> allPage = memberMapper.getAllPage(page,teamName, userName);
        return allPage;
    }
    @Override
    public IPage<MemberBelongVO> getUserMemberPage(MemberDTO memberDTO) {
        Integer pageNum = memberDTO.getPageNum();
        Integer pageSize = memberDTO.getPageSize();
        Integer teamId = memberDTO.getTeamId();
        String college = memberDTO.getCollege();
        String userName = memberDTO.getUserName();
        IPage<MemberBelongVO> page = new Page<>(pageNum,pageSize);
        IPage<MemberBelongVO> belongPage = memberMapper.getBelongPage(page,teamId, college, userName);
         return belongPage;
    }

    public PageData parsePage(IPage<Map<String, Object>> p) {
        PageData pageData = new PageData(p.getCurrent(), p.getSize(), p.getTotal(), p.getRecords());
        return pageData;
    }

    @Override
    public Boolean delete(Member member) {
        //
        memberMapper.deleteById(member);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",member.getTeamId());
        wrapper.setSql("number = number - 1");
        teamMapper.update(wrapper);
        return true;
    }

    @Override
    public Boolean addMember(MemberAddDTO memberAddDTO) {
        //通过名字查找用户id，
        QueryWrapper qw=new QueryWrapper();
        qw.eq("name",memberAddDTO.getName());
        Student student = studentMapper.selectOne(qw);
        if(student==null){
            return false;
        }
        //判断是否是社长，只要看他是否在member表存在
        QueryWrapper meqw=new QueryWrapper();
        meqw.eq("stu_id",student.getId());
        Member member1 = memberMapper.selectOne(meqw);
        if(member1!=null){
            return false;
        }
        Member member=new Member();
        member.setStuId(student.getId());
        member.setTeamId(memberAddDTO.getTeamId());
        memberMapper.insert(member);
        //社团成员+1
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",memberAddDTO.getTeamId());
        wrapper.setSql("number = number + 1");
        teamMapper.update(wrapper);
        return true;
    }

    @Override
    public Boolean isJoinThisTeam(MemberAddDTO memberAddDTO) {
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.eq("team_id",memberAddDTO.getTeamId());
        qw.eq("stu_id",memberAddDTO.getStuId());
        Long count = memberMapper.selectCount(qw);
        if(count>0){
            return true;
        }
        return false;
    }

    @Override
    public List<MemberBelongVO> getTeamMember(Integer id) {
        List<MemberBelongVO> teamMember = memberMapper.getTeamMember(id);
        return teamMember;
    }

    @Override
    public List<Map<String, Object>> getMemCollege(Integer teamId) {
        List<Map<String, Object>> memCollege = memberMapper.getMemCollege(teamId);
        return memCollege;
    }

    @Override
    public Result exitTeam(Member member) {
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.eq("team_id",member.getTeamId());
        qw.eq("stu_id",member.getStuId());
        Member member1 = memberMapper.selectOne(qw);
        if(member1==null){
            return Result.warn("系统出错，未找到该用户");
        }
        memberMapper.deleteById(member1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",member.getTeamId());
        wrapper.setSql("number = number - 1");
        teamMapper.update(wrapper);
        return Result.success("退出成功","退出成功");
    }




}
